package dream.web;

import com.alibaba.fastjson.JSON;
import dream.pojo.Article;
import dream.pojo.User;
import dream.service.ArticleService;
import dream.util.SessionUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


@WebServlet("/google")
public class GoogleServlet extends HttpServlet {
    private ArticleService articleService = new ArticleService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String val = req.getParameter("val");

        List<Article> articles = articleService.SelectByVal(val);

        if(articles != null){
            // 转为JSON
            String jsonString = JSON.toJSONString(articles);

            resp.setContentType("text/json;charset=utf-8");
            resp.getWriter().write(jsonString);
        }else{
            resp.getWriter().write("0");
        }
    }
}
